package com.xq.shop.controller.user;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xq.shop.po.CartGood;
import com.xq.shop.po.Order;
import com.xq.shop.po.OrderItem;
import com.xq.shop.po.Product;
import com.xq.shop.po.Receiver;
import com.xq.shop.po.User;
import com.xq.shop.service.user.IBaseService;
import com.xq.shop.service.user.ICartGoodService;
import com.xq.shop.service.user.IOrderService;
import com.xq.shop.service.user.IProductService;
import com.xq.shop.service.user.IReceiverService;
import com.xq.shop.util.Const;
import com.xq.shop.util.UserServiceFactory;
import com.xq.shop.vo.RespMsg;
import com.xq.shop.vo.SearchObject;
import com.xq.shop.vo.SearchValue.Pattern;

@Controller
@RequestMapping("/user/")
public class OrderCtrl extends BaseCtrl<Order>{

	private IOrderService orderService = UserServiceFactory.getOrderService();
	private IReceiverService receiverService = UserServiceFactory.getReceiverService();
	private IProductService productService = UserServiceFactory.getProductService();
	private ICartGoodService cartGoodService = UserServiceFactory.getCartGoodService();
	
	@Override
	public IBaseService<Order> getService() {
		return orderService;
	}

	@RequestMapping("order_list.do")
	@ResponseBody
	public String list(SearchObject search, HttpServletRequest request) {
		
		User user = (User) request.getSession().getAttribute(Const.User_Session);
		search.addCondition("user_id", user.getId(), Pattern.EQUAL);
		return super.list(search, request);
	}
	
	@RequestMapping("order_check.do")
	public ModelAndView checkOrder(Long[] product_id, Integer[] product_count, Double total, HttpServletRequest request){
		
		if(product_id == null || product_id.length < 1 || product_count == null 
				|| product_count.length < 1 || product_id.length != product_count.length){
			//TODO 
		}
		
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.User_Session);
		List<Receiver> receiverList= receiverService.getByUserID(user.getId());
		ModelAndView view = new ModelAndView("order/info");
		view.addObject("receiverList", receiverList);
		view.addObject("product_id", product_id);
		view.addObject("product_count", product_count);
		view.addObject("total", total);
		return view;
	}
	
	@RequestMapping("order_new.do")
	public ModelAndView newOrder(Long[] product_id, Long[] product_count, Double total, Long receiver_id, HttpServletRequest request){
		
		
		//TODO 处理异常
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Const.User_Session);
		Receiver receiver = receiverService.get(receiver_id);
		if(receiver == null){
			System.out.println("送货地址不存在");
			return new ModelAndView("home");
		} 
		Order order = new Order();
		order.setAddress(receiver.getAddress());
		order.setContact(receiver.getContact());
		order.setPostcode(receiver.getPostcode());
		order.setTel(receiver.getTel());
		order.setSn(System.currentTimeMillis() + "");
		
		order.setUser_id(user.getId());
		
		OrderItem orderItem;
		Product product;
		List<Product> product_List = productService.get(product_id);
		for(int i=0, len=product_List.size(); i<len; i++){
			
			product = product_List.get(i);
			orderItem = new OrderItem();
			orderItem.setProduct_id(product.getId());
			orderItem.setProduct_name(product.getName());
			orderItem.setQuantity(product_count[i]);
			//TODO 处理异常
			orderItem.setOrder_sn(order.getSn());
			orderItem.setPrice(product.getPrice());
			order.getItems().add(orderItem);
		}
		orderService.save(order);
		
		cartGoodService.delete(user.getId(), product_id);
		
		ModelAndView view = new ModelAndView("order/payment");
		view.addObject("order", order);
		return view;
	}
}
